/**
 * 0. Project	: com.dnbns.Receiver 프로젝트
 *
 * 1. FileName	: AlarmReceiver.java
 * 2. Package	: com.dnbns.Receiver
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 12. 24. 오후 1:55:00
 * 5. Comment	: 
 */

package com.dnbns.bodywater;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.dnbns.bodywater.R;
import com.dnbns.lib.CodeUtil;
import com.dnbns.lib.RbPreference;

public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context mContext, Intent intent) {
//	    //현재 실행중인 Activity PackageName 가져오기
//	    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
//	    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
//	    ComponentName topActivity = taskInfo.get(0).topActivity;
//	    //현재 어플이 실행중이지 않을때.
//  		if(!topActivity.getPackageName().equals("com.dnbns.bodywater")){
//  			Log.d("TEST", "AlarmReceiver Go : "+intent.getStringExtra("time"));
//  		}else{
//  			Log.d("TEST", "AlarmReceiver No : "+intent.getStringExtra("time"));
//  		}
		
		int hour = intent.getIntExtra("hour", 0);
		int minute = intent.getIntExtra("minute", 0);

		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal2.set(Calendar.HOUR_OF_DAY, hour);
		cal2.set(Calendar.MINUTE, minute);
		cal2.set(Calendar.SECOND, 0);

		//30초를 더했을때 현재 값 보다 클경우에만 Notification을 실행한다.
		if(cal.getTimeInMillis() < (cal2.getTimeInMillis() + 30*1000)){
			Log.d("TEST", "aaa : "+hour+":"+minute);
			CodeUtil.getInstance(mContext);
			CodeUtil.getInstance(mContext);
			showNotification(mContext, R.drawable.ic_launcher, "BodyWater", 
					CodeUtil.addZero(hour)+":"+CodeUtil.addZero(minute) + " 너는 지금 물이 먹고 싶어진다.!!");
		}
	}
	
	private void showNotification(Context mContext, int mIcon, String mTitle, String mText) {
		RbPreference pref = new RbPreference(mContext);

		if(pref.getValue("alarm", false)){
			Intent contentIntent = new Intent(mContext, MainActivity.class);
			contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
					                | Intent.FLAG_ACTIVITY_CLEAR_TOP 
					                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, contentIntent, PendingIntent.FLAG_ONE_SHOT);
			NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

		    builder.setSmallIcon(mIcon)
				.setContentTitle(mTitle)
				.setContentText(mText)
				.setWhen(System.currentTimeMillis())
				.setAutoCancel(true)
				.setContentIntent(mPendingIntent)
				.build();

			if(pref.getValue("vibrate", false)){
				Vibrator vibe = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
				vibe.vibrate(2000);
			}
			
			NotificationManager nm = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		    // Hide the notification after its selected
		    Notification notification = builder.build();
		    nm.notify(123, notification);

		}
	}
}
